package com.sykj.why.controller.front.category;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.AuxiliaryCategoryTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/6/19
 * @desc 前端辅助分类控制访问层
 */

@RestController
@RequestMapping("/front/category")
@Slf4j
@Api(value = "/front/category",description = "前端辅助分类控制访问层")
public class FrontAuxiliaryCategoryController {

    @Autowired
    private AuxiliaryCategoryTService auxiliaryCategoryTService;

    /**
     * 通过父级查询子级辅助分类
     * @param pid
     * @return
     */
    @RequestMapping(value = "/findByPid/{pid}",method = RequestMethod.GET)
    @ApiOperation(value ="/findByPid/{pid}",notes = "通过父级查询子级辅助分类")
    public ResponseEntity findByPid(@PathVariable String pid) {
        try {
            return Response.success("查询成功", auxiliaryCategoryTService.findByPid(pid));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("通过父级查询子级辅助分类出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 前端树状图
     * @param pid
     * @return
     */
    @RequestMapping(value = "/findTree/{pid}",method = RequestMethod.GET)
    @ApiOperation(value ="/findTree/{pid}",notes = "前端树状图")
    public ResponseEntity findTree(@PathVariable String pid) {
        try {
            return Response.success("查询成功", auxiliaryCategoryTService.findTree(pid));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("前端树状图出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
